<script setup lang="ts">
// App.vue
import { useChatStore } from './store/chat'
import { storeToRefs } from 'pinia'
import { onMounted, watch, ref, nextTick } from 'vue'

const chatStore = useChatStore()
const { elderlyModeEnabled } = storeToRefs(chatStore)
const forceUpdate = ref(0)

// 确保初始化时加载正确的模式
onMounted(() => {
  // 从localStorage直接读取，确保与store同步
  const savedMode = localStorage.getItem('elderlyModeEnabled')
  if (savedMode !== null) {
    const isElderlyMode = savedMode === 'true'
    if (elderlyModeEnabled.value !== isElderlyMode) {
      elderlyModeEnabled.value = isElderlyMode
    }
  }
  
  // 立即应用类
  applyElderlyModeClass(elderlyModeEnabled.value)
  
  // 监听本地存储变化
  window.addEventListener('storage', handleStorageChange)
})

// 监听适老化模式变化
watch(elderlyModeEnabled, (newValue) => {
  console.log('适老化模式变化被监听到:', newValue)
  
  // 强制组件重新渲染
  forceUpdate.value++
  
  // 应用适老化模式类
  applyElderlyModeClass(newValue)
}, { immediate: true })

// 应用适老化模式类
function applyElderlyModeClass(isEnabled: boolean) {
  document.documentElement.classList.toggle('elderly-mode-html', isEnabled)
  document.body.classList.toggle('elderly-mode', isEnabled)
  
  // 强制重绘
  nextTick(() => {
    document.body.style.padding = isEnabled ? '0.1px' : '0'
    setTimeout(() => {
      document.body.style.padding = '0'
    }, 20)
  })
}

// 处理存储变化
function handleStorageChange(event: StorageEvent) {
  console.log('存储变化:', event.key, event.newValue)
  
  if (event.key === 'elderlyModeEnabled') {
    const newValue = event.newValue === 'true'
    if (elderlyModeEnabled.value !== newValue) {
      elderlyModeEnabled.value = newValue
    }
    // 强制组件重新渲染
    forceUpdate.value++
  } else if (event.key === 'app_last_update') {
    // 强制组件重新渲染
    forceUpdate.value++
  }
}
</script>

<template>
  <div id="app" :class="{ 'elderly-mode': elderlyModeEnabled }" :key="forceUpdate">
    <router-view v-slot="{ Component }">
      <keep-alive>
        <component :is="Component" />
      </keep-alive>
    </router-view>
  </div>
</template>

<style>
html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
    Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background-color: #f7f7f7;
  color: #333;
  height: 100%;
  width: 100%;
  transition: all 0.3s ease;
}

#app, .app-container {
  height: 100%;
  width: 100%;
  transition: all 0.3s ease;
}

* {
  box-sizing: border-box;
}

/* 适老化模式全局样式 */
.elderly-mode {
  font-size: 18px !important;
  transition: all 0.3s ease;
}

.elderly-mode-force-update {
  animation: flash 0.5s;
}

@keyframes flash {
  0% { background-color: rgba(76, 175, 80, 0.1); }
  50% { background-color: rgba(76, 175, 80, 0.2); }
  100% { background-color: rgba(76, 175, 80, 0); }
}

.elderly-mode-html {
  font-size: 16px;
}

.elderly-mode .van-nav-bar__title {
  font-size: 22px !important;
}

.elderly-mode .van-nav-bar__text {
  font-size: 18px !important;
}

.elderly-mode .van-button {
  font-size: 18px !important;
  height: auto !important;
  padding: 10px 16px !important;
}

.elderly-mode .van-field__control {
  font-size: 18px !important;
}

.elderly-mode .van-action-sheet__item,
.elderly-mode .van-action-sheet__header {
  font-size: 20px !important;
  padding: 16px !important;
}

.elderly-mode .van-popup {
  font-size: 18px !important;
}

.elderly-mode .van-icon {
  font-size: 120% !important;
}

.elderly-mode .van-cell {
  font-size: 18px !important;
  padding: 14px 16px !important;
}

.elderly-mode p, 
.elderly-mode span, 
.elderly-mode div {
  line-height: 1.5 !important;
}

.elderly-mode button {
  min-height: 44px !important;
}

.elderly-mode input, 
.elderly-mode textarea {
  font-size: 18px !important;
}

/* 模式切换提示样式 */
.mode-change-toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 16px 24px;
  border-radius: 8px;
  font-size: 18px;
  z-index: 9999;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  animation: fadeIn 0.3s ease;
  text-align: center;
  min-width: 200px;
}

.elderly-mode .mode-change-toast {
  font-size: 24px;
  padding: 20px 30px;
  min-width: 250px;
}

.mode-change-toast.fade-out {
  animation: fadeOut 0.5s ease forwards;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translate(-50%, -60%); }
  to { opacity: 1; transform: translate(-50%, -50%); }
}

@keyframes fadeOut {
  from { opacity: 1; transform: translate(-50%, -50%); }
  to { opacity: 0; transform: translate(-50%, -40%); }
}
</style>
